package element.synt;

import ast.AstItem;
import ast.DataSignature;
import ast.Expression;
import ast.PrimaryDataSignature;
import element.base.Synt;
import element.base.Word;
import element.base.WordType;

public class PrimaryExp extends Synt {
    @Override
    public AstItem toAst() {
        Word firstWord = findFirst();
        if (firstWord.isWord(WordType.LPARENT)) {
            Exp exp = (Exp) getNodes().get(1);
            return exp.toAst();
        } else {
            PrimaryDataSignature signature = new PrimaryDataSignature();
            signature.type = DataSignature.DataType.PRIMARY;
            if (firstWord.isWord(WordType.NUMBER)) {
                String numStr = findFirst().getContent();
                if (numStr.contains(".")) {
                    Double num = Double.valueOf(numStr);
                    signature.value = num;
                } else {
                    Integer num = Integer.valueOf(numStr);
                    signature.value = num;
                }
            } else if (firstWord.isWord(WordType.CHARCON)) {
                String charStr = findFirst().getContent();
                Character ch = charStr.charAt(1);
                signature.value = ch;
            } else if (firstWord.isWord(WordType.STRCON)) {
                signature.value = findFirst().getContent();
            } else if (firstWord.isWord(WordType.TRUETK)) {
                signature.value = true;
            } else if (firstWord.isWord(WordType.FALSETK)) {
                signature.value = false;
            }

            Expression expression = new Expression();
            expression.operator = Expression.OperatorType.OP_ATOM;
            expression.operands.add(signature);
            expression.allUnaryExp.add(expression);
            return expression;
        }
    }
}
